-- Copyright (c) 2024 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
--
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local log = require 'mc.logging'
local custom_msg = require 'messages.custom'

local m = {}

local function get_recommended_policy(recommended_policy_names, recommended_policies, influences)
    if type(recommended_policy_names) ~= 'userdata' or type(recommended_policies) ~= 'userdata' or
      type(influences) ~= 'userdata'  then
        log:erro('Recommended policy data type error.')
        error(custom_msg.InternalError())
    end
    local size = #recommended_policy_names
    if size ~= #recommended_policies or size ~= #influences then
        log:erro('Recommended policies size error.')
        error(custom_msg.InternalError())
    end
    local ret, obj = {}
    for i = 1, size, 1 do
        obj = {}
        obj.PolicyName = recommended_policy_names[i]
        obj.PolicyValue = recommended_policies[i]
        obj.Effect = influences[i]
        table.insert(ret, obj)
    end
    return ret
end

local function get_current_policy(current_policy_names, current_policies)
    if type(current_policy_names) ~= 'userdata' or type(current_policies) ~= 'userdata' then
        log:erro('Current policy data type error.')
        error(custom_msg.InternalError())
    end
    local size = #current_policy_names == #current_policies and #current_policy_names or nil
    if not size then
        log:erro('Current policies size error.')
        error(custom_msg.InternalError())
    end
    local ret, obj = {}
    for i = 1, size, 1 do
        obj = {}
        obj.PolicyName = current_policy_names[i]
        obj.PolicyValue = current_policies[i]
        table.insert(ret, obj)
    end
    return ret
end

function m.get_policy_guidances(input)
    local ret, object = {}
    for _, item in pairs(input) do
        object = {}
        object.ComponentType = item.Component
        object.EnergySavingScore = item.HealthPercent and item.HealthPercent or 0
        object.Current = get_current_policy(item.CurrentPolicyName, item.CurrentPolicies)
        object.Recommended = get_recommended_policy(item.RecommendedPolicyName, item.RecommendedPolicies,
                                    item.Influence)
        table.insert(ret, object)
    end
    return ret
end

function m.check_min_work_psu_count_valid(input)
    if type(input) == 'number' and (input < 1 or input > 254) then
        local err = custom_msg.PropertyValueOutOfRange(string.format("%.0f", input), "%MinWorkPSUCount", "1",
                    tostring(input))
        err.RelatedProperties = {'#/MinWorkPSUCount'}
        error(err)
    end
    return true
end

return m
